﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace INTREPWEB.Models
{
    public class EmbarqueViewModel
    {
        public String Id { get; set; }

        [Required]
        [DataType(DataType.Date)]
        public DateTime DataInicio { get; set; }

        [DataType(DataType.Time)]
        public DateTime HoraInicio { get; set; }

        [DataType(DataType.Date)]
        public DateTime DataFim { get; set; }

        [DataType(DataType.Time)]
        public DateTime HoraFim { get; set; }

        public bool Corrente { get; set; }

        [Display(Name="Navio")]
        public int NavioId { get; set; }
        public SelectList Navios { get; set; }

        public EmbarqueViewModel()
        {
            DataInicio = DataFim = HoraInicio = HoraFim = DateTime.Now;

            using (INTREPDB db = new INTREPDB())
            {
                var list = db.Navios.ToList()
                    .Select(x => new SelectListItem
                    {
                        Text = x.Nome,
                        Value = x.Id.ToString()
                    });
                Navios = new SelectList(list, "Value", "Text");
            }
        }

        public EmbarqueViewModel(string id)
        {
            using (INTREPDB db = new INTREPDB())
            {
                var embarque = db.Embarques.Find(id);
                DataInicio = HoraInicio = embarque.DataInicio;
                DataFim = HoraFim = embarque.DataFim;

                var list = db.Navios.ToList()
                    .Select(x => new SelectListItem
                    {
                        Text = x.Nome,
                        Value = x.Id.ToString()
                    });
                Navios = new SelectList(list, "Value", "Text");
            }
        }
    }
}